//
//  HJChatMessageVideoCell.m
//  XWC
//
//  Created by Hello on 2022/10/25.
//

#import "HJChatMessageVideoCell.h"
#import "HJChatMessageVideoLayout.h"
#import "HJChatMessageModel.h"

@interface HJChatMessageVideoCell()

@property (nonatomic, strong) UIImageView *playImageView;
@property (nonatomic, weak) UIView *markContentView;
@property (nonatomic, strong) HJCircleProgress *circleProgress;

@end


@implementation HJChatMessageVideoCell

 
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
      
        UIImageView *imageView = [UIImageView addToView:self.containerView];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.addCornerRadius(5);
        imageView.backgroundColor = UIColorMake(0xf3f3f3);
        _contentImageView = imageView;
        
        UIImageView *playImageView = [UIImageView addToView:imageView];
        playImageView.image = [UIImage imageNamed:@"im_play"];
        playImageView.alpha = 0.8;
        playImageView.contentMode = UIViewContentModeScaleAspectFill;
        _playImageView = playImageView;
        
        UIView *markContentView = [UIView addToView:self.containerView];
        markContentView.addCornerRadius(5);
        markContentView.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.2];
        _markContentView = markContentView;
        markContentView.alpha = 0.9;
        
        CGFloat circleProgressW = 40;
        self.circleProgress = [[HJCircleProgress alloc] initWithFrame:CGRectMake(0, 0, circleProgressW, circleProgressW)];
        [imageView addSubview:self.circleProgress];
        self.circleProgress.progress = 0;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdateChatFileUploadProgressNotification:) name:updateChatFileUploadProgressNotification object:nil];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _contentImageView.frame = CGRectMake(0, 0, self.messageLayout.containerViewRect.size.width, self.messageLayout.containerViewRect.size.height);
    _playImageView.frame = CGRectMake((_contentImageView.tj_width - 40) * 0.5, (_contentImageView.tj_height - 40) * 0.5, 40, 40);
    
    _markContentView.frame = _contentImageView.frame;
    self.circleProgress.tj_x = (_markContentView.tj_width - self.circleProgress.tj_width) * 0.5;
    self.circleProgress.tj_y = (_markContentView.tj_height - self.circleProgress.tj_height) * 0.5;
}

- (void)setMessageLayout:(HJChatMessageLayout *)messageLayout {
    [super setMessageLayout:messageLayout];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:messageLayout.messageModel.thumbnailPath]) {
        _contentImageView.image = [UIImage imageWithContentsOfFile:messageLayout.messageModel.thumbnailPath];
    } else {
        _contentImageView.image = [UIImage imageNamed:loadVideoDefaultImageName];
    }
    self.indicatorView.hidden = YES;
    [self.indicatorView stopAnimating];
    [self updateProgress:messageLayout.messageModel.progress];
}

- (void)updateProgress:(CGFloat)progress {
    
    if (self.messageLayout.messageModel.msgStatus == HJMessageStatusSending) {
        _markContentView.hidden = NO;
        _circleProgress.hidden = NO;
        _circleProgress.progress = progress;
    } else {
        _circleProgress.hidden = YES;
        _markContentView.hidden = YES;
        _circleProgress.progress = 0;
        self.messageLayout.messageModel.progress = 0;
    }
}

- (void)handleUpdateChatFileUploadProgressNotification:(NSNotification *)not {
    NSDictionary *info = not.object;
    
    NSString *msgId = info[@"msgId"];
    CGFloat progress = [info[@"progress"] floatValue];
    if (![msgId isEqualToString:self.messageLayout.messageModel.msgId]) return;
    [self updateProgress:progress];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
